package com.creative.xcx.controller;

import com.creative.xcx.domain.ThematicSelection;
import com.creative.xcx.domain.vo.ThematicSelectionVo;
import com.creative.xcx.service.ThematicSelectionService;
import com.creative.core.controller.BaseController;
import com.creative.core.domain.AjaxResult;
import com.creative.core.page.TableDataInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.alibaba.excel.EasyExcel;
import java.util.List;
import java.util.Map;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @description 专题精选
 * @author ling
 * @date 2025-02-26
 */
@RestController
@RequestMapping(value = "/ThematicSelection")
public class ThematicSelectionController extends BaseController {

    @Resource
    private ThematicSelectionService thematicSelectionService;

    /**
     * 新增
     * @author ling
     * @date 2025/02/26
     **/
    @PostMapping
    public AjaxResult insert(@RequestBody ThematicSelection thematicSelection){
        return toAjax(thematicSelectionService.insert(thematicSelection));
    }

    /**
     * 刪除单个
     * @author ling
     * @date 2025/02/26
     **/
    @DeleteMapping("/{tsId}")
    public AjaxResult delete(@PathVariable("tsId") String tsId){
        return toAjax(thematicSelectionService.delete(tsId));
    }

    /**
     * 批量刪除
     * @author ling
     * @date 2025/02/26
     **/

    @DeleteMapping("/batch")
    public AjaxResult deleteBatch(@RequestBody String[]   ids) {
        return toAjax(thematicSelectionService.deleteBatch(ids));
    }


    /**
     * 更新
     * @author ling
     * @date 2025/02/26
     **/
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ThematicSelection thematicSelection){
        return toAjax(thematicSelectionService.update(thematicSelection));
    }


    /**
     * 查询 根据主键 id 查询
     * @author ling
     * @date 2025/02/26
     **/
    @GetMapping(value ="/{tsId}")
    public AjaxResult selectById(@PathVariable("tsId") String tsId){

        if (tsId == null){
            return error("参数为空");
        }
        return success(thematicSelectionService.selectById(tsId.trim()));
    }
    /**
     * 查询 分页查询(带条件)
     * @author ling
     * @date 2025/02/26
     **/
    @GetMapping("/list")
    public TableDataInfo selectThematicSelectionList(ThematicSelection thematicSelection) {
        return thematicSelectionService.selectThematicSelectionList(thematicSelection);
    }

    /**
     * 查询小程序专题精选数据
     *
     * @param thematicSelection 专题精选
     * @return {@link TableDataInfo }
     * @author ling
     * @date 2025/03/02
     */
    @GetMapping("/mp/list")
    public AjaxResult selectThematicSelectionMpList(ThematicSelection thematicSelection) {
        logger.info("查询小程序专题精选数据 分页查询(带条件)：{}", thematicSelection);
        return success(thematicSelectionService.selectMpThematicSelectionList(thematicSelection));
    }

    /**
     * 批量导出
     * @author ling
     * @date 2025/02/26
     **/
    @PostMapping("/export")
    public void export(@RequestBody String[]   ids, HttpServletResponse response) throws IOException{
        //        判断ids是否为空
        if (ids == null || ids.length == 0) {
            // ids 为空或未初始化时执行的代码
            throw new RuntimeException("ids为空");
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("专题精选" + System.currentTimeMillis(), StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ThematicSelectionVo.class)
                .sheet("专题精选")
                .doWrite(() -> {
                    // 查询数据
                    return thematicSelectionService.exportBatch(ids);
                });
        logger.info("导出成功");
    }

    /**
     * 查询类型列表
     *
     * @return {@link AjaxResult }
     * @author ling
     * @date 2025/03/01
     */
    @GetMapping("/type")
    public AjaxResult selectTypeList(){
        return success(thematicSelectionService.selectTypeList());
    }
}
